﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace MPMovieCatalog.Configuration.Sections
{
    public sealed class ScriptSection : PropertySection
    {
        [ConfigurationProperty("scripts")]
        private PropertyElement m_scripts
        {
            get
            {
                PropertyElement host = (PropertyElement)base["scripts"];
                return host;
            }

            set
            {
                base["scripts"] = value;
            }
        }

        public ArrayList Scripts
        {
            get
            {
                ArrayList scriptFiles = new ArrayList();
                foreach (string str in m_scripts.Value.Split(';'))
                {
                    if (str != string.Empty)
                    {
                        string[] scriptFileArray = str.Split(',');
                        if (scriptFileArray.Length > 0)
                        {
                            ScriptFile scriptFile = new ScriptFile();
                            scriptFile.FileName = scriptFileArray[0];
                            if (scriptFileArray.Length > 1)
                            {
                                scriptFile.Name = scriptFileArray[1];
                                if (scriptFileArray.Length > 2)
                                {

                                    scriptFile.Language = scriptFileArray[2];
                                    if (scriptFileArray.Length > 3)
                                    {
                                        scriptFile.Limit = int.Parse(scriptFileArray[3]);
                                    }
                                }
                            }
                            scriptFiles.Add(scriptFile);
                        }
                    }
                }

                return scriptFiles;
            }
            set
            {
                StringBuilder sb = new StringBuilder();
                foreach (ScriptFile scriptFile in value)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(";");
                    }
                    sb.Append(scriptFile.FileName);
                    sb.Append(",");
                    sb.Append(scriptFile.Name);
                    sb.Append(",");
                    sb.Append(scriptFile.Language);
                    sb.Append(",");
                    sb.Append(scriptFile.Limit);
                }
                SetProperty("scripts", sb.ToString());
            }
        }
    }
}
